import request from '@/utils/http'

export interface BaseEntity {
  id: number
  create_time: string
  update_time: string
}

// GenBusiness 响应（与后端 GenBusinessResponse 对齐）
export interface GenBusiness extends BaseEntity {
  app_name: string
  table_name: string
  doc_comment?: string
  table_comment?: string
  class_name?: string
  schema_name?: string
  filename?: string
  default_datetime_column: boolean
  api_version: string
  gen_path?: string
  remark?: string
}

// 列表响应
export interface GenBusinessListResponse {
  total: number
  items: GenBusiness[]
  page: number
  size: number
  pages: number
}


// 创建请求体
export interface GenBusinessCreate extends Omit<GenBusiness, keyof BaseEntity> { }

// 更新请求体
export interface GenBusinessUpdate extends Partial<GenBusinessCreate> { }

// 查询参数
export interface GenBusinessQuery {
  current?: number
  size?: number
  table_name?: string
  table_comment?: string
  start_time?: string
  end_time?: string
}

/**
 * 获取代码生成业务列表（分页）
 */
export function fetchGenBusinessList(data: GenBusinessQuery) {
  return request.post<GenBusinessListResponse>({
    url: '/code-generator/businesses/list',
    data
  })
}

/**
 * 获取单个代码生成业务
 */
export function fetchGenBusiness(id: number) {
  return request.get<GenBusiness>({
    url: `/code-generator/businesses/detail/${id}`
  })
}

/**
 * 创建代码生成业务
 */
export function createGenBusiness(data: GenBusinessCreate) {
  return request.post<GenBusiness>({
    url: '/code-generator/businesses/create',
    data,
    showSuccessMessage: true
  })
}

/**
 * 更新代码生成业务
 */
export function updateGenBusiness(id: number, data: GenBusinessUpdate) {
  return request.put<GenBusiness>({
    url: `/code-generator/businesses/update/${id}`,
    data,
    showSuccessMessage: true
  })
}

/**
 * 删除代码生成业务
 */
export function deleteGenBusiness(id: number) {
  return request.del<{ message: string }>({
    url: `/code-generator/businesses/delete/${id}`,
    showSuccessMessage: true
  })
}